const {
  request
} = require("../../utils/index.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:false,  //搜索框收缩
    isheat: true, //最热
    iscollect: false, //最新
    inputValue: null,
    resultList: [],
    Eliteshare: {
      pageNum: 1, //页码
      pageSize: 10, //条数
      searchLabel: "heat", //搜索标签
      searchName: "",  //名称查询
      isMy:"false"  //只看自己
    }
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom:async  function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载  1秒
    let that = this;
    if (this.data.Eliteshare.pageNum >= this.data.resultList.lastPage) {
      wx.showToast({
        title: '滑到底了',
        icon: 'none'
      })
      wx.hideNavigationBarLoading() //完成停止加载
      return;
    }
    that.setData({
      "Eliteshare.pageNum": ++this.data.Eliteshare.pageNum
    })
    let par = {
      url: "hw/portal/articles/ds/page?token=" + wx.getStorageSync('token'),
      method: "GET",
      data: that.data.Eliteshare,
      header: {
        "Content-Type": "application/json"
      },
    }
    let datainfo =await request(par);
    if (datainfo.data != undefined) {
      let lao = that.data.resultList.list  //将老数据存入
      if (datainfo.data.list) {
        let list = [...lao,...datainfo.data.list]  //新老数据拼接
        that.setData({
          resultList:datainfo.data,  //重新赋值
          "resultList.list": list
        })
      } else {
        //滑倒底了
      }
      wx.hideNavigationBarLoading() //完成停止加载
    }
  },
  
  //搜索框搜索
  result(e){
    this.setData({
      "Eliteshare.searchName":e.detail
    });
    this.init();
  },

  //搜索框收缩状态
  search(){
    this.setData({
      search:true
    })
  },

  //只看最热
  heat(e) {
    this.setData({
      "Eliteshare.searchLabel": "heat",
      isheat: !this.data.isheat,
      iscollect: !this.data.iscollect
    })
    this.init();
  },
  //只看收藏
  collect(e) {
    this.setData({
      "Eliteshare.searchLabel": "collect",
      iscollect: !this.data.iscollect,
      isheat: !this.data.isheat,
    })
    this.init();
  },

  clean: function () {
    var _this = this
    setTimeout(function () {
      _this.setData({
        inputValue: '',
      })
    }, 100)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
  },
  //精英分享列表
  async init() {
    try {
      let that = this;
      let par = {
        url: "hw/portal/articles/ds/page?token=" + wx.getStorageSync('token'),
        method: "GET",
        data: that.data.Eliteshare,
        header: {
          "Content-Type": "application/json"
        }
      }
      let datainfo = await request(par);
      console.log(datainfo)
      if (datainfo.status == 100) {
        that.setData({
          resultList: datainfo.data
        })
      }
    } catch (error) {
      console.log(error);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
/**
 * 生命周期函数--监听页面显示
 */
onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})